﻿package org.xhs.ui.controls
{
    import baidu.ui.constants.*;
    import baidu.ui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class LabelButton extends Button
    {
        protected var _value:Object;
        public var textField:TextField;
        protected var _labelPlacement:String = "right";
        protected var _toggle:Boolean = false;
        protected var _icon:DisplayObject;
        protected var _label:String = "Label";
        protected var _autoSize:Object = false;
        public static var defaultStyles:Object = {iconSkin:"Button_Icon_Skin", padding:5, gap:5, embedFonts:false};

        public function LabelButton()
        {
            return;
        }// end function

        override public function get classStyles() : Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }// end function

        override public function get selected() : Boolean
        {
            return this._toggle ? (_selected) : (false);
        }// end function

        override public function set selected(param1:Boolean) : void
        {
            _selected = param1;
            if (this._toggle)
            {
                invalidate(Invalidation.STATE);
            }
            return;
        }// end function

        public function get icon() : Object
        {
            return getStyle("iconSkin");
        }// end function

        public function set icon(param1) : void
        {
            setStyle("iconSkin", param1);
            return;
        }// end function

        public function get label() : String
        {
            return this._label;
        }// end function

        public function set label(param1:String) : void
        {
            this._label = param1;
            if (this.textField.text != this._label)
            {
                this.textField.text = this._label;
            }
            invalidate(Invalidation.SIZE);
            invalidate(Invalidation.STYLES);
            return;
        }// end function

        public function get labelPlacement() : String
        {
            return this._labelPlacement;
        }// end function

        public function set labelPlacement(param1:String) : void
        {
            this._labelPlacement = param1;
            invalidate(Invalidation.SIZE);
            return;
        }// end function

        public function get toggle() : Boolean
        {
            return this._toggle;
        }// end function

        public function set toggle(param1:Boolean) : void
        {
            if (!param1 && super.selected)
            {
                this.selected = false;
            }
            this._toggle = param1;
            if (this._toggle)
            {
                addEventListener(MouseEvent.CLICK, this.toggleSelected, false, 0, true);
            }
            else
            {
                removeEventListener(MouseEvent.CLICK, this.toggleSelected);
            }
            invalidate(Invalidation.STATE);
            return;
        }// end function

        public function get value() : Object
        {
            return this._value;
        }// end function

        public function set value(param1:Object) : void
        {
            this._value = param1;
            return;
        }// end function

        public function get autoSize() : Object
        {
            return this._autoSize;
        }// end function

        public function set autoSize(param1:Object) : void
        {
            this._autoSize = param1;
            invalidate(Invalidation.SIZE);
            return;
        }// end function

        protected function toggleSelected(event:MouseEvent) : void
        {
            this.selected = !this.selected;
            dispatchEvent(new Event(Event.CHANGE, true));
            return;
        }// end function

        override protected function initUI() : void
        {
            super.initUI();
            this.textField = new TextField();
            this.textField.type = TextFieldType.DYNAMIC;
            this.textField.selectable = false;
            addChild(this.textField);
            return;
        }// end function

        override protected function draw() : void
        {
            if (this.textField.text != this._label)
            {
                this.label = this._label;
            }
            if (isInvalid(Invalidation.STYLES, Invalidation.STATE))
            {
                drawBackground();
                this.drawIcon();
                this.drawTextFormat();
                invalidate(Invalidation.SIZE, false);
            }
            if (isInvalid(Invalidation.SIZE))
            {
                this.drawLayout();
            }
            validate();
            return;
        }// end function

        protected function drawIcon() : void
        {
            var _loc_2:String = null;
            var _loc_1:* = this._icon;
            this._icon = getSkinInstance(getStyleValue("iconSkin")) as DisplayObject;
            if (this._icon != null)
            {
                addChildAt(this._icon, 1);
                if (this._icon is MovieClip)
                {
                    _loc_2 = enabled ? (mouseState) : ("disabled");
                    if (this.selected)
                    {
                        _loc_2 = "selected" + _loc_2.substr(0, 1).toUpperCase() + _loc_2.substr(1);
                    }
                    (this._icon as MovieClip).gotoAndStop(frameDict[_loc_2]);
                }
            }
            if (_loc_1 != null && _loc_1 != this._icon)
            {
                removeChild(_loc_1);
            }
            return;
        }// end function

        protected function drawTextFormat() : void
        {
            var _loc_1:* = getStyleValue(enabled ? ("textFormat") : ("disabledTextFormat")) as TextFormat;
            if (_loc_1 != null)
            {
                this.textField.setTextFormat(_loc_1);
                this.textField.defaultTextFormat = _loc_1;
            }
            this.setEmbedFont();
            return;
        }// end function

        protected function setEmbedFont() : void
        {
            var _loc_1:* = getStyleValue("embedFonts");
            if (_loc_1 != null)
            {
                this.textField.embedFonts = _loc_1;
            }
            return;
        }// end function

        override protected function drawLayout() : void
        {
            var _loc_10:Number = NaN;
            var _loc_11:Number = NaN;
            var _loc_1:* = Number(getStyleValue("padding"));
            var _loc_2:* = Number(getStyleValue("gap"));
            var _loc_3:* = this._icon == null ? (BUIConstants.TOP) : (this._labelPlacement);
            this.textField.height = this.textField.textHeight + 4;
            var _loc_4:* = this.textField.textWidth + 4;
            var _loc_5:* = this.textField.textHeight + 4;
            var _loc_6:* = this._icon == null ? (0) : (this._icon.width);
            var _loc_7:* = this._icon == null ? (0) : (this._icon.height);
            var _loc_8:* = this._icon == null ? (0) : (this._icon.width + _loc_2);
            var _loc_9:* = this._icon == null ? (0) : (this._icon.height + _loc_2);
            this.textField.visible = this.label.length > 0;
            if (this.autoSize)
            {
                this.textField.autoSize = "left";
                if (_loc_3 == BUIConstants.BOTTOM || _loc_3 == BUIConstants.TOP)
                {
                    _width = Math.max(_loc_4, _loc_6) + _loc_1 * 2;
                    _height = _loc_5 + _loc_9 + _loc_1 * 2;
                }
                else
                {
                    _width = _loc_4 + _loc_8 + _loc_1 * 2;
                    _height = Math.max(_loc_5, _loc_7) + _loc_1 * 2;
                }
            }
            else
            {
                this.textField.autoSize = "none";
            }
            if (this._icon != null)
            {
                this._icon.x = Math.round((_width - this._icon.width) / 2);
                this._icon.y = Math.round((_height - this._icon.height) / 2);
            }
            if (this.textField.visible == false)
            {
                this.textField.width = 0;
                this.textField.height = 0;
            }
            else if (_loc_3 == BUIConstants.BOTTOM || _loc_3 == BUIConstants.TOP)
            {
                _loc_10 = Math.max(0, Math.min(_loc_4, _width - 2 * _loc_1));
                _loc_11 = Math.min(_height - 2, _loc_5);
                var _loc_12:* = _loc_10;
                _loc_4 = _loc_10;
                this.textField.width = _loc_12;
                var _loc_12:* = _loc_11;
                _loc_5 = _loc_11;
                this.textField.height = _loc_12;
                this.textField.x = Math.round((_width - _loc_4) / 2);
                this.textField.y = Math.round((_height - this.textField.height - _loc_9) / 2 + (_loc_3 == BUIConstants.BOTTOM ? (_loc_9) : (0)));
                if (this._icon != null)
                {
                    this._icon.y = Math.round(_loc_3 == BUIConstants.BOTTOM ? (this.textField.y - _loc_9) : (this.textField.y + this.textField.height + _loc_2));
                }
            }
            else
            {
                _loc_10 = Math.max(0, Math.min(_loc_4, _width - _loc_8 - 2 * _loc_1));
                var _loc_12:* = _loc_10;
                _loc_4 = _loc_10;
                this.textField.width = _loc_12;
                this.textField.x = Math.round((_width - _loc_4 - _loc_8) / 2 + (_loc_3 != BUIConstants.LEFT ? (_loc_8) : (0)));
                this.textField.y = Math.round((_height - this.textField.height) / 2);
                if (this._icon != null)
                {
                    this._icon.x = Math.round(_loc_3 != BUIConstants.LEFT ? (this.textField.x - _loc_8) : (this.textField.x + _loc_4 + _loc_2));
                }
            }
            super.drawLayout();
            return;
        }// end function

    }
}
